package com.zhangfb95.crosschain.console.command;

import com.zhangfb95.crosschain.console.enums.CommandEnum;
import com.zhangfb95.crosschain.infra.model.Result;
import com.zhangfb95.crosschain.sdk.CrossChainClient;
import com.zhangfb95.crosschain.sdk.infra.model.api.response.server.rpcchain.RpcListChainNameApiRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangfubing
 * @since 2023/12/21
 */
@Component
public class ListChainNameCommand implements Command {

    @Autowired
    private CrossChainClient crossChainClient;

    @Override
    public CommandEnum command() {
        return CommandEnum.LIST_CHAIN_NAME;
    }

    @Override
    public boolean handle(String[] args) {
        if (isHelp(args)) {
            System.out.println("Usage: listChainName");
            return false;
        }

        Result<RpcListChainNameApiRes> apiRes = crossChainClient.listChainName();
        if (apiRes.isSuccess()) {
            apiRes.getData().getChainNames().forEach(chainName -> {
                System.out.println("name: " + chainName);
            });
        } else {
            System.out.println("Result: " + apiRes.getMsg());
        }
        return false;
    }
}
